package com.kqb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kqb.common.domain.AjaxResult;
import com.kqb.common.domain.page.PageResult;
import com.kqb.mapper.ProductMapper;
import com.kqb.pojo.Product;
import com.kqb.pojo.dto.ProductDto;
import com.kqb.service.IProductService;
import com.kqb.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Pageable;
import java.util.List;

/**
 * @author 杉果树皮
 * @date 2025/3/3 19:45
 * @description:
 */
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public AjaxResult save(Product product) {
        product.setCreateBy((String) ThreadLocalUtils.getThreadLocalUtils().getCurrentUser().get("account"));
        return productMapper.save(product) != 0 ? AjaxResult.success("添加产品成功") : AjaxResult.error("添加产品失败");
    }

    @Override
    public AjaxResult selectByPage(ProductDto productDto) {
        PageHelper.startPage(productDto.getPageNum(), productDto.getPageSize());
        List<Product> products = productMapper.selectByPage(productDto);
        PageInfo<Product> productPageInfo = new PageInfo<>(products);
        return AjaxResult.success(new PageResult(productDto.getPageNum(), productDto.getPageSize(), productPageInfo.getTotal(), productPageInfo.getList()));
    }

    @Override
    public AjaxResult deleteById(Integer id) {
        return productMapper.deleteById(id) != 0 ? AjaxResult.success("删除产品成功") : AjaxResult.error("删除产品失败");
    }

    @Override
    public AjaxResult updateProductById(Product product) {
        return productMapper.updateProductById(product) != 0 ? AjaxResult.success("修改产品信息成功") : AjaxResult.error("修改产品信息失败");
    }
}
